package scene;

import geometrie.Point;
import geometrie.Rebouchage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Implementation of a Modele3D that represents a Prism. It's possible to
 * specify the number of sides of the prism. If a big number of sides is
 * specified (around 100, for example) the resulting Modele3D can be seen as a
 * cylinder.
 */
public class Prism implements Modele3D {
	private List<Facette3D> facettes;

	public Iterator<Facette3D> getFacettes() {
		return facettes.listIterator();
	}
	
	public void addTriangulation(Point[] polygon) {
		facettes.addAll(Rebouchage.rebouchageTrouRecCache(polygon));
	}

	/**
	 * Constructs a new Prism with the specified parameters
	 * 
	 * @param center
	 *            The prism's center
	 * @param height
	 *            The prism's height
	 * @param radius
	 *            The prism's radius (distance from the center to the faces)
	 * @param numPoints
	 *            the prism's number of sides. If a big number (around 100) is
	 *            specified, the resulting prism can be seen as a cylinder.
	 */
	public Prism(Point center, double height, double radius, int numPoints) {
		if (numPoints <= 0) {
			numPoints = 1;
		}
		double polygonAngle = (numPoints - 2) * 180 / numPoints;
		Point p1, p2, p3, p4;
		double x, y, z;
		x = center.getXCartesien();
		y = center.getYCartesien();
		z = center.getZCartesien();
		
		Point[] topPolygon = new Point[numPoints];
		Point[] bottomPolygon = new Point[numPoints];
		
		facettes = new ArrayList<Facette3D>(numPoints * 2);
		int i;
		double radAngle = Math.toRadians(180 - polygonAngle);
		for (i = 0; i < numPoints; i++) {
			p1 = new Point(x + radius * Math.sin(i * radAngle), y, z + radius
					* Math.cos(i * radAngle));
			p2 = new Point(x + radius * Math.sin((i + 1) * radAngle), y, z
					+ radius * Math.cos((i + 1) * radAngle));
			p3 = new Point(x + radius * Math.sin(i * radAngle), y + height, z
					+ radius * Math.cos(i * radAngle));
			p4 = new Point(x + radius * Math.sin((i + 1) * radAngle), y
					+ height, z + radius * Math.cos((i + 1) * radAngle));
			facettes.add(new Facette3D(p1, p2, p3));
			facettes.add(new Facette3D(p2, p3, p4));
			topPolygon[i] = p3;
			bottomPolygon[i] = p1;
		}
		addTriangulation(topPolygon);
		addTriangulation(bottomPolygon);
	}
}
